<?php

namespace api\controller;

use common\controller\VerifyController;
use common\exception\LogicException;
use common\service\TagRelationService;
use think\Request;
use support\SingletonTrait;

/**
 * 标签关联
 */
class TagRelationController extends VerifyController
{
    use SingletonTrait;

    /**
     * @var TagRelationService
     */
    private $tagRelationService;

    public function __construct()
    {
        parent::__construct();
        $this->tagRelationService = new TagRelationService();
    }

    /**
     * 标签绑定
     * @param Request $request
     * @return \think\Response|void
     * @throws \Exception
     */
    public function bind(Request $request)
    {
        try {
            $param = $request->param();
            $res = $this->tagRelationService->bind($param['data'], $request->getUserId());
            return json(success_response("", $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 更新标签绑定
     * @param Request $request
     * @return \think\Response|void
     * @throws \Exception
     */
    public function updateBind(Request $request)
    {
        try {
            $param = $request->param();
            $res = $this->tagRelationService->updateBind($param['data'], $request->getUserId());
            return json(success_response("", $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 解除标签绑定
     * @param Request $request
     * @return \think\Response|void
     */
    public function unbind(Request $request)
    {
        try {
            $param = $request->param();
            $res = $this->tagRelationService->unbind($param['data']);
            return json(success_response("", $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 根据关联id获取标签
     * @param Request $request
     * @return \think\Response|void
     */
    public function getTagsByRelationIds(Request $request)
    {
        try {
            $param = $request->param();
            $res = $this->tagRelationService->getTagsByRelationIds($param['param']);
            return json(success_response("", $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }
}
